package Content;

use KIT_DB;

sub new
{
	my ($class, $db_name)	= @_;		
	my $self		= {};
	$self->{group_id}	= 0;
	$self->{realm_id}	= 0;

	bless $self, 'Content';
	return $self;

}

sub newFromDB
{
    my ($class, $content_id)	= @_;

    my $self	= {};

    my $stmt    = "SELECT id, content, date_created, title, subject, stylesheet, active FROM content WHERE id = $content_id";
    my $kit_db  = new KIT_DB();
    my $hashref = $kit_db->GetSingleRow($stmt);

    $self->{content_id} =  $hashref->{content_id};
    $self->{content}    =  $hashref->{content};
    $self->{date_created}   =   $hashref->{date_created};
    $self->{title}      =  $hashref->{title};
    $self->{subject}    =  $hashref->{subject};
    $self->{stylesheet} =  $hashref->{stylesheet};
    $self->{active}     =  $hashref->{active};
    $self->{group_id}   =  0;
    $self->{realm_id}   =  0;

    bless $self, 'Content';
    return $self;
}

#	-- See also SaveNewContent
sub Update
{
    my ($self)	= @_;

    my $content_id	= $self->{'content_id'};
    my $content	= $self->{'content'};		$content =~ s/\'/\'\'/g;
    my $subject	= $self->{'subject'};		$subject =~ s/\'/\'\'/g;
    my $title	= $self->{'title'};		$title 	 =~ s/\'/\'\'/g;
    my $stylesheet	= $self->{'stylesheet'};
    my $sender_email= $self->{'sender_email'};
    my $active	= $self->{'active'};

    my $kit_db  = new KIT_DB();
    my $stmt	= "UPDATE content SET content='$content', subject='$subject', title='$title', stylesheet='$stylesheet, active = $active'
                        WHERE  id = $content_id";

    my $kit_db  = new KIT_DB();
    return $kit_db->Update($stmt);
}

sub UpdateActivity
{
    my ($self)	= @_;

    my $content_id	= $self->{'content_id'};
    my $active	= $self->{'active'};
    
    my $stmt	= "UPDATE content SET active = $active WHERE id = $content_id";
    my $kit_db  = new KIT_DB();
    return $kit_db->Update($stmt);

}

sub InsertNew
{
    my ($self)	= @_;

    my $kitDates	= new KitDates();

    my $content	= $self->{'content'};		$content =~ s/\'/\\'/g;
    my $subject	= $self->{'subject'};		$subject =~ s/\'/\\'/g;
    my $title	= $self->{'title'};		$title 	 =~ s/\'/\\'/g;
    my $stylesheet	= $self->{'stylesheet'};
    my $sender_email= $self->{'sender_email'};
    my $realm_id	= $self->{'realm_id'};

    my $date_created= $kitDates->getDateTime();

    my $stmt	= "INSERT INTO content (content, subject, title, date_created, stylesheet, realm_id) 
                        VALUES ('$content', '$subject', '$title', '$date_created', '$stylesheet', $realm_id)";

    my $kit_db  = new KIT_DB();
    return $kit_db->Insert($stmt);
}

sub GetContentList
{
    my $self		= shift;
    my $active		= shift;
    my @content_list	= ();

    # print "active = " . $active;
    $active     = "0,1" if ($active ne '0' && $active ne '1');

    my $stmt    = "SELECT 	id, subject, content, title, date_created, active 
                        FROM 	content 
                        WHERE 	active IN ($active)
                        AND		realm_id = " . $self->{realm_id} . " " .
                        "ORDER BY     id DESC";

    my $kit_db  = new KIT_DB();
    my @rows	= $kit_db->GetMultipleRows($stmt);
    foreach (@rows)
    {
        my %content_hash = (
            content_id	=> $_->[0],
            content		=> $_->[1],
            subject		=> $_->[2],
            title		=> $_->[3],
            date_created	=> $_->[4],
            active		=> $_->[5]
        );
        push @content_list, \%content_hash;	
    }
    return @content_list;
}

sub GetContentListByGroup
{
    my $self	= shift;
    my $group_id	= shift;
    my $active	= shift || '0,1';
    
    my $kit_db     = new KIT_DB();

    my @allContent	= ();

    my $stmt	= " SELECT  CT.id, CT.subject, CT.content, CT.title, CT.date_created, CT.active,
                            CP.id, CP.sender_email, CP.sender_name, CP.campaign_date
                    FROM    content AS CT, campaigns AS CP, groups AS G
                    WHERE   CT.active IN ($active) 
                    AND	    CP.group_id = $group_id
                    AND	    CT.id = CP.content_id
                    AND	    G.id = CP.group_id
                    AND	    CT.realm_id = " . $self->{realm_id} . " " .
                    "ORDER BY     CP.id DESC";

    my $kit_db  = new KIT_DB();
    my @rows   = $kit_db->GetMultipleRows($stmt);

	foreach (@rows)
	{
		use bytes;
		my $byte_size	= length($_->[1]);

		my %content_hash = (
			content_id	=> $_->[0],
			content		=> $_->[1],
			subject		=> $_->[2],
			title		=> $_->[3],
			date_created	=> $_->[4],
			active		=> $_->[5],
			content_size	=> $byte_size,
			campaign_id	=> $_->[6],
			sender_email	=> $_->[7],
			sender_name	=> $_->[8],
			campaign_date	=> $_->[9]
		);
		push @content_list, \%content_hash;	
	}
	return @content_list;
}

sub get_max_content_id 
{
    my $self    = shift;

#     my $sql     = "SELECT count(*) from content where realm_id = " . $self->{realm_id};
# 
#     if ($kit_db->GetSingleValue($sql) > 
    my $sql     = "SELECT max(id) from content where realm_id = " . $self->{realm_id};
    my $kit_db  = new KIT_DB();
    return $kit_db->GetSingleValue($sql);
}

1;